package mww.servlet.course;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.service.CourseService;
import mww.javabean.service.CourseServiceImpl;
import mww.javabean.service.TeacherCService;
import mww.javabean.service.TeacherCServiceImpl;
public class DelCourseServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CourseService courseService = new CourseServiceImpl();
		TeacherCService tcService = new TeacherCServiceImpl();
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String courseNo=request.getParameter("courseNo");
		try{
			//删除课程信息前判断从表中是否存在关联记录，若不存在关联进行删除，否则禁止删除
			if(tcService.checkTeacherCExistByCourseNo(courseNo)){
				session.setAttribute("message", "所要删除记录存在关联项，禁止删除课程信息！");
				request.getRequestDispatcher("/admin/listAllCourseServlet").forward(request, response);
			}else{
				courseService.delCourse(courseNo);
		  		session.setAttribute("message", "删除课程信息成功！");
		  		request.getRequestDispatcher("/admin/listAllCourseServlet").forward(request, response);
			}
		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
